package com.power.sifa.approve.executor;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.plf.common.utils.StringUtil;
import com.plf.core.base.model.EnumerationHelper;
import com.power.sifa.approve.dao.Apl_thirdperDao;
import com.power.sifa.approve.model.Apl_thirdper;
import com.power.sifa.approve.model.enumeration.Tp_gender;
import com.power.sifa.correction.model.enumeration.Tp_crc_penalty;
import com.power.sifa.correction.model.enumeration.Tp_crc_userEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 行政复议共有服务
 */
public class AdmreExecutor {

    @Resource
    private Apl_thirdperDao apl_thirdperDao;

    /**
     * 行政复议审批表意见和日期字段
     * @param formData
     * @throws ParseException
     */
    protected void ApprovalField(Map<String, Object> formData) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatT = new SimpleDateFormat("yyyy年M月d日");
        if (!StringUtil.isEmpty(formData.get("apl_admre_audit_firstdate"))) {
            formData.put("apl_admre_audit_firstdate", formatT.format(format.parse((String) formData.get("apl_admre_audit_firstdate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_admre_audit_seconddate"))) {
            formData.put("apl_admre_audit_seconddate", formatT.format(format.parse((String) formData.get("apl_admre_audit_seconddate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_admre_audit_thirddate"))) {
            formData.put("apl_admre_audit_thirddate", formatT.format(format.parse((String) formData.get("apl_admre_audit_thirddate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_admre_audit_fourthdate"))) {
            formData.put("apl_admre_audit_fourthdate", formatT.format(format.parse((String) formData.get("apl_admre_audit_fourthdate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_admre_audit_fivedate"))) {
            formData.put("apl_admre_audit_fivedate", formatT.format(format.parse((String) formData.get("apl_admre_audit_fivedate"))));
        }
    }

    /**
     * 审批表意见和日期字段
     * @param formData
     * @throws ParseException
     */
    protected void CorrectionField(Map<String, Object> formData) throws ParseException {
        // 性别
        if(null != formData.get("apl_condeploy_person_gender")) {
            formData.put("apl_condeploy_person_gender", Tp_gender.GENDERMAP.get(formData.get("apl_condeploy_person_gender")));
        }
        // 民族
        if(null != formData.get("apl_condeploy_person_nation")) {
            EnumerationHelper helper = SpringUtil.getBean(EnumerationHelper.class);
            List<Map<String, Object>> nationEnum = helper.getEnumerationValues("Tp_nation");
            String nationVal = null;
            if (CollectionUtil.isNotEmpty(nationEnum)) {
                for (Map<String, Object> nationMap : nationEnum) {
                    if (nationMap.containsKey("id") && nationMap.get("id").equals((Integer) formData.get("apl_condeploy_person_nation"))) {
                        nationVal = (String) nationMap.get("description");
                    }
                }
                formData.put("apl_condeploy_person_nation",nationVal);
            }
        }
        // 原判刑罚
        if(null != formData.get("apl_condeploy_person_penalty")) {
            formData.put("apl_condeploy_person_penalty", Tp_crc_penalty.PENALTYMAP.get(formData.get("apl_condeploy_person_penalty")));
        }
        // 矫正类别
        if(null != formData.get("apl_condeploy_person_crctype")) {
            formData.put("apl_condeploy_person_crctype", Tp_crc_userEnum.getTypeNameByValue((Integer) formData.get("apl_condeploy_person_crctype")));
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatT = new SimpleDateFormat("yyyy年M月d日");
        if (!StringUtil.isEmpty(formData.get("apl_condeploy_cert_startdate"))) {
            formData.put("apl_condeploy_cert_startdate", formatT.format(format.parse((String) formData.get("apl_condeploy_cert_startdate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_condeploy_cert_enddate"))) {
            formData.put("apl_condeploy_cert_enddate", formatT.format(format.parse((String) formData.get("apl_condeploy_cert_enddate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_condeploy_person_startdate"))) {
            formData.put("apl_condeploy_person_startdate", formatT.format(format.parse((String) formData.get("apl_condeploy_person_startdate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_condeploy_person_enddate"))) {
            formData.put("apl_condeploy_person_enddate", formatT.format(format.parse((String) formData.get("apl_condeploy_person_enddate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_condeploy_person_constartdate"))) {
            formData.put("apl_condeploy_person_constartdate", formatT.format(format.parse((String) formData.get("apl_condeploy_person_constartdate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_condeploy_person_conenddate"))) {
            formData.put("apl_condeploy_person_conenddate", formatT.format(format.parse((String) formData.get("apl_condeploy_person_conenddate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_condeploy_cert_countydate"))) {
            formData.put("apl_condeploy_cert_countydate", formatT.format(format.parse((String) formData.get("apl_condeploy_cert_countydate"))));
        }
        if (!StringUtil.isEmpty(formData.get("apl_condeploy_cert_citydate"))) {
            formData.put("apl_condeploy_cert_citydate", formatT.format(format.parse((String) formData.get("apl_condeploy_cert_citydate"))));
        }
    }

    /**
     * 查询第三人姓名
     * @param applyid
     * @return
     */
    protected String queryThirdper(String applyid) {
        List<Apl_thirdper> thilist = this.apl_thirdperDao.getByApplyid(applyid);
        if (!CollectionUtils.isEmpty(thilist)) {
            List<String> namelist = new ArrayList<>(thilist.size());
            for (Apl_thirdper v : thilist) {
                namelist.add(v.getName());
            }
            return StringUtils.join(namelist, "，");
        }
        return "";
    }
}
